package com.example.demo.proxy;

import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Home-pc
 * Date: 2024-02-23
 * Time: 17:21
 */
public class CGLIBInterceptor implements MethodInterceptor {
        //⽬标对象, 即被代理对象
        private Object target;
        public CGLIBInterceptor(Object target){
            this.target = target;
        }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                // 代理增强内容
                System.out.println("我是中介, 开始代理");
                //通过反射调⽤被代理类的⽅法
                Object retVal = methodProxy.invoke(target, objects);
                //代理增强内容
                System.out.println("我是中介, 代理结束");
                return retVal;
    }
}
